package br.ufc.great;

import javax.crypto.Mac;

import com.google.android.maps.GeoPoint;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {

	private static final int NOTIFICATION_ID = 1000;
	String[] infAboutPoint = { "", "", "" };
	MActivity mActivity;

	@Override
	public void onReceive(Context context, Intent intent) {

		String key = LocationManager.KEY_PROXIMITY_ENTERING;
		System.out.println("entrouu");

		boolean entering = intent.getBooleanExtra(key, false);

		if (entering) {
			Log.d(getClass().getSimpleName(), "entering");
		} else {
			Log.d(getClass().getSimpleName(), "exiting");
		}

		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				null, 0);
		Notification notification = createNotification();

		String stringProvider = mActivity.locationManager.getBestProvider(
				new Criteria(), true);// Cria os critérios de busca.
		Location location = mActivity.locationManager
				.getLastKnownLocation(stringProvider);

		if (location != null) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lng = (int) (location.getLongitude() * 1E6);
			GeoPoint lastPoint = new GeoPoint(lat, lng);
			Location aproximateLocation = mActivity.myOverlay
					.getAproximateGeoPointToShowNotification(lastPoint);
			if (aproximateLocation == null) {
				// Do Anything! :)
			} else {
				System.out.println("pa" + aproximateLocation.getLatitude());
				lat = (int) (aproximateLocation.getLatitude() * 1E6);
				lng = (int) (aproximateLocation.getLongitude() * 1E6);
				GeoPoint lastPointInDb = new GeoPoint(lat, lng);
				infAboutPoint = mActivity.readDBByPoint(lastPointInDb);
				notification.setLatestEventInfo(context,
						"Alerta de Proximidade!", infAboutPoint[0] + "-"
								+ infAboutPoint[1], pendingIntent);
				notificationManager.notify(NOTIFICATION_ID, notification);
			}
		}

	}

	private Notification createNotification() {
		Notification notification = new Notification();

		notification.icon = R.drawable.ufclogo;
		notification.when = System.currentTimeMillis();

		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.flags |= Notification.FLAG_SHOW_LIGHTS;

		notification.defaults |= Notification.DEFAULT_VIBRATE;
		notification.defaults |= Notification.DEFAULT_LIGHTS;

		notification.ledARGB = Color.WHITE;
		notification.ledOnMS = 1500;
		notification.ledOffMS = 1500;

		return notification;
	}

	public void fillOutInfAboutPoint(MActivity mActivity)
	{
		// Passar a referência da MActivity para que eu possa acessar o método ler do banco e do aproxPoint.
		/*
		 * System.out.println(title + description); infAboutPoint[0] = title;
		 * infAboutPoint[1] = description;
		 */
		this.mActivity = mActivity;
	}
}
